package com.youlu.campus.admin.system;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.SystemNickNameAndHeaderImage;
import com.youlu.campus.entity.VO.req.SystemNickNameAndHeaderImageQueryVO;
import com.youlu.campus.service.wechat.SystemNickNameAndHeaderImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Objects;

@RestController
public class SystemNickNameAndHeaderImageController {
    @Autowired
    private SystemNickNameAndHeaderImageService systemNickNameAndHeaderImageService;

    @PostMapping("/system-nick-image/list")
    public RestResult list(@RequestBody @Valid SystemNickNameAndHeaderImageQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<SystemNickNameAndHeaderImage> users = systemNickNameAndHeaderImageService.list(req);
        if (users == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            users = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(users).build();
    }


    @PostMapping("/system-nick-image/create")
    public RestResult create(@RequestBody @Valid SystemNickNameAndHeaderImage req) {
        String comId = AuthUtils.getComId();
        req.setTenantId(comId);
        req.setOptLoginName(AuthUtils.getLoginName());
        boolean re = systemNickNameAndHeaderImageService.create(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/system-nick-image/update")
    public RestResult update(@RequestBody @Valid SystemNickNameAndHeaderImage req) {
        String comId = AuthUtils.getComId();
        req.setTenantId(comId);
        boolean re = systemNickNameAndHeaderImageService.update(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/system-nick-image/delete")
    public RestResult delete(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = systemNickNameAndHeaderImageService.delete(id);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/system-nick-image/find-one")
    public RestResult findOne(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        SystemNickNameAndHeaderImage s = systemNickNameAndHeaderImageService.findOne(id);
        if (Objects.nonNull(s)) {
            return RestResultBuilder.builder().success(s).build();
        }
        return RestResultBuilder.builder().failure().build();
    }
}
